 package l2hc.gameserver.handler.voicecommands.impl;
 
 import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import l2hc.gameserver.Config;
import l2hc.gameserver.dao.CharacterDAO;
import l2hc.gameserver.dao.RefferalDao;
import l2hc.gameserver.data.htm.HtmCache;
import l2hc.gameserver.data.xml.holder.ItemHolder;
import l2hc.gameserver.handler.voicecommands.IVoicedCommandHandler;
import l2hc.gameserver.model.Player;
import l2hc.gameserver.model.World;
import l2hc.gameserver.network.serverpackets.components.CustomMessage;
import l2hc.gameserver.scripts.Functions;
import l2hc.gameserver.templates.item.ItemTemplate;
/**
 * @author 4ipolino
 */
 public class RefferalSystem extends Functions implements IVoicedCommandHandler
 {
   private String[] _commandList = { "ref" };
 
   @SuppressWarnings({ "unchecked", "rawtypes" })
   public static CopyOnWriteArrayList<RefferalDao.CharInfo> all_info = new CopyOnWriteArrayList();
 
   public boolean useVoicedCommand(String command, Player activeChar, String args)
   {
     if (!Config.ALLOW_REFFERAL_SYSTEM)
       return false;
     if (command.equals("ref"))
     {
       if (args != null)
       {
         String[] param = args.split(" ");
         if (param.length == 2)
         {
           if (param[0].equalsIgnoreCase("add"))
           {
             if (CharacterDAO.getInstance().getObjectIdByName(param[1]) == 0) {
               activeChar.sendMessage(new CustomMessage("common.Admin.RefSystem.InvalidName", activeChar, new Object[0]));
             } else if (CharacterDAO.getInstance().getObjectIdByName(param[1]) == activeChar.getObjectId()) {
               activeChar.sendMessage(new CustomMessage("common.Admin.RefSystem.InvalidSelf", activeChar, new Object[0]));
             } else if (RefferalDao.getInstance().countReffs(param[1]) >= Config.MAX_REFFERALS_PER_CHAR) {
               activeChar.sendMessage(new CustomMessage("common.Admin.RefSystem.LimitReached", activeChar, new Object[0]));
             }
             else
             {
               int reffered_id = CharacterDAO.getInstance().getObjectIdByName(param[1]);
               RefferalDao.CharInfo char_info = new RefferalDao.CharInfo(reffered_id, param[1], activeChar.getObjectId(), activeChar.getName());
               all_info.add(char_info);
               activeChar.sendMessage(new CustomMessage("common.Admin.RefSystem.RefSuccess", activeChar, new Object[0]));
             }
           }
           else if (param[0].equalsIgnoreCase("remove"))
           {
             int obj = Integer.parseInt(param[1]);
             RefferalDao.getInstance().removeBlock(obj);
             activeChar.sendMessage(new CustomMessage("common.Admin.RefSystem.DelSuccess", activeChar, new Object[0]));
           }
         }
       }
     }
     String newApp = "";
 
     if (activeChar.getOnlineTime() / 360L < Config.MIN_ONLINE_TIME) {
       newApp = newApp + "Система доступна после " + Config.MIN_ONLINE_TIME + " часов игры<br>";
     }
     else if (activeChar.getLevel() < Config.MIN_REFF_LEVEL) {
       newApp = newApp + "Система доступна с " + Config.MIN_REFF_LEVEL + " уровня<br>";
     }
     else if (!RefferalDao.getInstance().isCharReffered(activeChar.getObjectId()))
     {
       newApp = newApp + "<edit var=\"new_reff\" width=150> <br>";
       newApp = newApp + "<button value=\"Добавить\" action=\"bypass -h user_ref add $new_reff \" width=150 height=15><br>";
     }
     else {
       newApp = newApp + "Вы уже задали реферала <br>";
     }
     newApp = newApp + "Список рефералов:<br>";
 
     List<String> name_list = RefferalDao.getInstance().getCharNamesByRef(activeChar.getObjectId());
     int i = 1;
     newApp = newApp + "<table width=200><tr><td>#</td><td></td><td>Имя</td><td></td><td>Удалить</td></tr>";
     for (String name : name_list)
     {
       int id = RefferalDao.getReffererIdByName(name);
       newApp = newApp + "<tr><td>" + i + "</td><td></td><td>" + name + "</td><td></td>";
       newApp = newApp + "<td><button value=\"Удалить\" action=\"bypass -h user_ref remove " + id + " \" width=75 height=15></td></tr>";
       i++;
     }
     newApp = newApp + "</table>";
 
     String dialog = HtmCache.getInstance().getNotNull("command/refferal.htm", activeChar);
     dialog = dialog.replace("%Reff%", newApp);
 
     show(dialog, activeChar);
     return true;
   }
 
   public static void applyBonus(Player rewarded, int id, int count)
   {
     int r_id = RefferalDao.getInstance().getRefferedIdByRefferer(rewarded.getObjectId());
     if (r_id == 0)
       return;
     Player player = World.getPlayer(r_id);
     if (player == null) {
       return;
     }
     double _count = count * Config.REF_PERCENT_GIVE;
     _count = Math.round(_count);
 
     ItemTemplate item = ItemHolder.getInstance().getTemplate(id);
     player.sendMessage(new CustomMessage("common.Admin.RefSystem.CharNotice", player, new Object[0]).addNumber(_count).addString(item.getName()).addString(rewarded.getName()));
 
     addItem(player, id, (long) _count);
   }
 
   public String[] getVoicedCommandList() {
     return this._commandList;
   }
 }
